<?php

include_once LIBRARIES_TOOLS_XTEMPLATE;
include_once LIBRARIES_TOOLS_UTILITY;
include_once LIBRARIES_ENTITIES_DATABASE;
include_once LIBRARIES_ENTITIES_ARTICLES;

class BlockNewsSlideShowController {

    public function invoke() {

        $viewFile = 'block/block_news_slideshow/views/news_slideshow.html';


        $xtemplate = new XTemplate($viewFile);

        // tải danh sách slideshow
        $highlightArticles = $this->getHighlightArticles();

        // nếu số lượng slideshow lớn hơn 0 thì tiến hành lặp từng dòng dữ liệu
        if (count($highlightArticles) > 0) {
            for ($i = 0; $i < count($highlightArticles); $i++) {
                if ($i <= 4) {
                    $xtemplate->assign('INDEX', $i);
                    $xtemplate->assign('BRIEF_TITLE', Utility::subString($highlightArticles[$i]->getTitle(), 60, true));
                    $xtemplate->assign('TITLE', Utility::subString($highlightArticles[$i]->getTitle(), 120, true));
                    $xtemplate->assign('IMAGE_LINK', $highlightArticles[$i]->getImage());
                    $xtemplate->assign('DESCRIPTION', $highlightArticles[$i]->getDescription());
                    $xtemplate->assign('ARTICLE_ALIAS', $highlightArticles[$i]->getAlias());
                    $xtemplate->assign('ARTICLE_ID', $highlightArticles[$i]->getID());
                    $xtemplate->parse('main.large_content_list');
                    $xtemplate->parse('main.small_content_list');
                }
            }
        }
        $xtemplate->parse('main');
        $xtemplate->out('main');
    }

    private function getHighlightArticles() {
        $db = new Database();
        try {
            $article = new Articles();
            return $article->getHighLightList($db);
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            exit();
        }
    }

}

?>
